<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../../assert/css/subpagesCss/usetMessage.css" th:href="@{/assert/css/subpagesCss/usetMessage.css}">
    <link href="../../../assert/css/plugin/toastr/toastr.min.css" rel="stylesheet" th:href="@{/assert/css/plugin/toastr/toastr.min.css}">
    <link href="../../../assert/js/lib/jqwidgets-ver4.0.0/jqwidgets/styles/jqx.base.css" rel="stylesheet"
          th:href="@{/assert/js/lib/jqwidgets-ver4.0.0/jqwidgets/styles/jqx.base.css}"/>
    <link rel="stylesheet" href="../../../assert/css/index.css" th:href="@{/assert/css/index.css(v_=${@commonController.getSysConfigByPK('CACHE_CODE')})}">
</head>
<body style="min-width:950px;overflow: auto">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins">
        <div style="min-width: 1207px;overflow: auto">
            <div class="user_box" style="width:450px;margin-right:10px;">
                <h2>基本信息</h2>
                <form id="baseForm">
                    <div class="user_message">
                        <div class="user_body">
                            <div class="group">
                                <label for="userName" class="label">当前用户：</label>
                                <span id="userName">admin</span>
                            </div>
                            <div class="group">
                                <label for="oddPassword" class="label">班组：</label>
                                <span id="seatGroup"></span>
                            </div>
                            <div class="group">
                                <label for="newPassword" class="label">工号：</label>
                                <span id="account"></span>
                            </div>
                            <div class="group">
                                <label for="newPassword" class="label">手机号：</label>
                                <span id="phone"></span>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="user_box">
                <h2>修改密码</h2>
                <form id="validateForm">
                    <div class="user_message">
                        <div class="user_body">
                            <div class="group">
                                <label for="oddPassword" class="label">当前密码：</label>
                                <input id="oddPassword" type="password" name="oddPassword" autocomplete="off">
                                <input type="passward" style="display: none" id="password" autocomplete="off">
                            </div>
                            <div class="group">
                                <label for="newPassword" class="label">新密码：</label>
                                <input id="newPassword" type="password" name="newPassword" autocomplete="off">
                            </div>
                            <div class="group">
                                <label for="firmPassword" class="label">确认新密码：</label>
                                <input id="firmPassword" type="password" name="firmPassword" autocomplete="off">
                            </div>
                        </div>
                        <div class="user_footer">
                            <button id="save" type="button">保存</button>
                            <button id="reset" type="button">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

</div>
<a id="basePath" th:href="@{/}"></a>
</body>
<script src="../../../assert/js/jquery.js" th:src="@{/assert/js/jquery.js(v_=${@commonController.getSysConfigByPK('CACHE_CODE')})}"></script>
<script src="../../../assert/js/lib/jqwidgets-ver4.0.0/jqwidgets/jqxcore.js"
        th:src="@{/assert/js/lib/jqwidgets-ver4.0.0/jqwidgets/jqxcore.js}"></script>
<script src="../../../assert/js/lib/jqwidgets-ver4.0.0/jqwidgets/jqxvalidator.js"
        th:src="@{/assert/js/lib/jqwidgets-ver4.0.0/jqwidgets/jqxvalidator.js}"></script>
<script src="../../../assert/js/lib/jqwidgets-ver4.0.0/jqwidgets/localization.js" th:src="@{/assert/js/lib/jqwidgets-ver4.0.0/jqwidgets/localization.js}"></script>
<script src="../../../assert/js/plugin/toastr/toastr.min.js" th:src="@{/assert/js/plugin/toastr/toastr.min.js}"></script>
<script src="../../../assert/js/jsencrypt.min.js" th:src="@{/assert/js/jsencrypt.min.js(v_=${@commonController.getSysConfigByPK('CACHE_CODE')})}"></script>
<script src="../../../assert/js/common.js" th:src="@{/assert/js/common.js(v_=${@commonController.getSysConfigByPK('CACHE_CODE')})}"></script>
<script>
    //项目名称
    var basePath = $("#basePath").attr("href");
    // 获取加密的公匙
    var encrypt = new JSEncrypt();
    $.get(basePath + "rest/user/pass/key", function (data) {
        if(data != ""){
            encrypt.setPublicKey(data);
        }
    });
    var validate = {
        init: function () {
            this.initValidator();
            this.initSave();
            this.load();
        }
        , load: function () {
            var url = basePath + "/rest/users/passwordSale";
            $.ajax({
                url: url,
                type:"post",
                success: function (msg) {
                        var data = msg.data;
                        $("#userName").attr("data-id", data.id);
                        $("#userName").html(data.userName);
                        $("#password").val(data.password);
                        getData.init();
                }
            });
        }
        , initValidator: function () {
            $("#validateForm").jqxValidator({
                rules: [
                    {
                        input: '#oddPassword',
                        message: '当前密码不能为空!',
                        action: 'keyup,blur',
                        rule: function (input, commit) {
                            return input.val().trim() != ''
                        }
                    }
                    , {
                        input: '#newPassword',
                        message: '新密码不能为空!',
                        action: 'keyup,blur',
                        rule: function (input, commit) {
                            return input.val().trim() != ''
                        }
                    }
                    , {
                        input: '#firmPassword',
                        message: '确认密码不能为空!',
                        action: 'keyup,blur',
                        rule: function (input, commit) {
                            return input.val().trim() != ''
                        }
                    }
                    , {
                        input: '#oddPassword',
                        message: '当前密码输入不正确!',
                        action: 'keyup,blur',
                        rule: function (input, commit) {
                            // 对密码进行加密
                            var encrypted = encrypt.encrypt(input.val());
                            // 比较密码
                            validate = true;
                            var url = basePath + "/rest/users/passwordCompare";
                            var data = {
                                password: encrypted
                            };
                            $.ajax({
                                url: url,
                                data: data,
                                async: false,
                                success: function (msg) {
                                    if (msg == "fault") {
                                        validate = false;
                                    } else if (msg == "success") {
                                        validate = true;
                                    }
                                }
                            });
                            return validate;
                        }
                    }
                    , {
                        input: '#newPassword',
                        message: '6-30个字符，建议使用字母、数字和符号两种以上的组合!',
                        action: 'keyup,blur',
                        rule: function (input, commit) {
                            var result = true;
                            var val = $(input).val();
                            if ((val && val != "")) {
                                if (!(/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,30}$/.test(val))) {
                                    result = false;
                                }
                            }
                            return result;
                        }
                    }
                    , {
                        input: '#firmPassword',
                        message: '该密码与新密码不一致!',
                        action: 'keyup,blur',
                        rule: function (input, commit) {
                            validate = true;
                            if (input.val() !== $("#newPassword").val()) {
                                validate = false;
                            }
                            return validate;
                        }
                    }
                ],
//                hintType: "label"
            });
        }
        , initSave: function () {
            $("#save").unbind().on("click", function () {
                $("#validateForm").jqxValidator("validate");
            });
            $("#validateForm").on("validationSuccess", function (event) {
                var url = basePath + "/rest/users/userUpdatePassword2";
                // 测试过程中发现集团人员id并没有获取到值 修改后台接口 不传参id-- by lihongli 2019/2/23
                // var id = $("#userName").attr("data-id");
                var password = $("#newPassword").val();
                // 加密
                var encrypted = encrypt.encrypt(password);
                var data = {
                    // id: id,
                    password: encrypted
                };
                $.post(url, data, function (msg) {
                    if (msg != null && msg.code == 0) {//修改密码成功
                        toastrCog.success(msg.msg, 1000);
                        $("#reset").click();
                        parent.pswTip.init();
                    } else {
                        toastrCog.error(msg.msg, 1000)
                    }
                });
            });
        }
    };
    var resize = {
        init: function () {
            var winW = $(window).width();
            var winH = $(window).height();
            $(".ibox").width(winW - 20);
            $(".ibox").height(winH - 24);
            $(window).resize(function () {
                resize.me();
            })
        },
        me: function () {
            var winW = $(window).width();
            var winH = $(window).height();
            $(".ibox").width(winW - 20);
            $(".ibox").height(winH - 24);

        }
    };
    var getData = {
        init: function () {
            var url = basePath + "/rest/users/getUserByPK";
            var data = {
                id: $("#userName").attr("data-id")
            };
            $.ajax({
                url: url,
                data: data,
                type:"post",
                success: function (msg) {
                    if(msg.group){
                        var name = msg.group.name;
                        $("#seatGroup").html(name);
                    }
                    var account = msg.account;
                    var mobileno = msg.mobileno;

                    $("#account").html(account);
                    $("#phone").html(mobileno);
                }
            });
        }
    }
    $(function () {
        resize.init();
        toastrCog.cog();
        validate.init();

        $("#reset").on("click", function () {
            $("input").val("");
            $("#validateForm").jqxValidator('hideHint', "#oddPassword");
            $("#validateForm").jqxValidator('hideHint', "#firmPassword");
            $("#validateForm").jqxValidator('hideHint', "#newPassword");
        })
    })
</script>
</html>